OPENMP ?= 1

ifeq (,$(CNC_INSTALL_DIR))
CNC_INSTALL_DIR2=$(shell dirname $(CURDIR))
CNC_INSTALL_DIR=$(shell dirname $(CNC_INSTALL_DIR2))
endif

NUMA_INCLUDE=-I/usr/src/linux-headers-1$(shell uname -r)/include/ -I/usr/include/
NUMA_LIB=-L/usr/lib/ -lnuma

ARCH := intel64

SOURCES_C = $(wildcard *.c)
SOURCES_CPP = $(wildcard *.cpp)

TARGETS := stencil

DEST_OBJS_C :=$(SOURCES_C:.c=.o)
DEST_OBJS_CPP :=$(SOURCES_CPP:.cpp=.opp)

OPT_LEVEL := -O3
ifeq ($(DEBUG), 1)
OPT_LEVEL := -O0 -g
endif

CXX = clang++
OPTCXX := -std=c++11 $(OPT_LEVEL)
OPT := $(OPT_LEVEL)
CC = clang

ifeq ($(GCC),1)
OPT = -fast
CXX = g++
OPT := $(OPT_LEVEL)
OPTCXX := $(OPT_LEVEL) -std=c++11 
CC=gcc
endif

ifeq ($(ICC),1)
CXX = icpc
OPTCXX := $(OPT_LEVEL) -fp-model precise -vec_report=3
CC = icc
OPT := $(OPT_LEVEL) -fp-model precise
endif

ifeq ($(OPENMP),1)
OPENMP_OPT += -fopenmp
ifeq ($(ICC),1)
OPENMP_OPT = -openmp
endif
OPT += $(OPENMP_OPT)
OPTCXX += $(OPENMP_OPT)
endif

CNC_RUN_OPTIONS :=
ifeq ($(DIST),1)
CNC_RUN_OPTIONS += -D_DIST_
endif

# For some reason, removing this include path results in a 10x slowdown on my machine ...
PAPI_INCLUDE=-I/usr/local/include/
ifeq ($(PAPI),1)
CNC_RUN_OPTIONS += -D_PAPI_
PAPI_LIBRARY += -L/usr/local/lib/ -lpapi
endif

ifeq ($(HUGE_TLB),1)
CNC_RUN_OPTIONS += -DHUGE_TLB
endif



ifeq ($(DEBUG),1)
OPT += -DDEBUG=1
OPTCXX += -DDEBUG=1
endif

ifeq ($(DEBUG_NUMA),1)
OPT += -DDEBUG_NUMA=1
OPTCXX += -DDEBUG_NUMA=1
endif

ifeq ($(DEBUG_NUMA_MEM),1)
OPT += -DDEBUG_NUMA_MEM=1
OPTCXX += -DDEBUG_NUMA_MEM=1
endif


all:  stencil

%.opp: %.cpp
	$(CXX) -c $(OS_INCLUDE) -I$(CNC_INSTALL_DIR)/include $(PAPI_INCLUDE) $(OPTCXX) $(CNC_RUN_OPTIONS) -o $@ $< 

%.o: %.c
	$(CC) -c -I$(CNC_INSTALL_DIR)/include $(OPT) $(CNC_RUN_OPTIONS) -o $@ $< 

stencil: $(DEST_OBJS_CPP) $(DEST_OBJS_C)
	$(CXX) -o $@ $(DEST_OBJS_C) $(DEST_OBJS_CPP) -std=c++0x -lcnc -ltbb -ltbbmalloc -lpthread -lrt $(NUMA_LIB) $(PAPI_LIBRARY) -fopenmp

clean:
	make clean -C simple_expression_template
	rm -f $(TARGETS) $(DEST_OBJS_C) $(DEST_OBJS_CPP) *~ a.out dmp err */*~
